package com.yikes.yygh.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yikes.yygh.common.result.PageResult;
import com.yikes.yygh.common.utils.object.BeanUtils;
import com.yikes.yygh.controller.form.HospitalSetAddForm;
import com.yikes.yygh.controller.form.HospitalSetEditForm;
import com.yikes.yygh.controller.form.HospitalSetForm;
import com.yikes.yygh.controller.vo.HospitalSetVO;
import com.yikes.yygh.pojo.entity.HospitalSet;
import com.yikes.yygh.mapper.HospitalSetMapper;
import com.yikes.yygh.service.HospitalSetService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yikes.yygh.web.utils.password.MD5;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

/**
 * <p>
 * 医院设置表 服务实现类
 * </p>
 *
 * @author guanrong.yin
 * @since 2025-05-01
 */
@Slf4j
@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper, HospitalSet> implements HospitalSetService {

    @Override
    public List<HospitalSetVO> findAll() {

        List<HospitalSet> list = this.list();

        return BeanUtils.toBean(list, HospitalSetVO.class);
    }

    @Override
    public PageResult<HospitalSetVO> findPage(HospitalSetForm form) {

        Page<HospitalSet> hospitalSetPage = lambdaQuery()
                .like(StrUtil.isNotEmpty(form.getHosname()), HospitalSet::getHosname, form.getHosname())
                .eq(StrUtil.isNotEmpty(form.getHoscode()), HospitalSet::getHoscode, form.getHoscode())
                .page(Page.of(form.getPageNum(), form.getPageSize()));

        PageResult<HospitalSetVO> pageResult = PageResult.build(hospitalSetPage.getTotal(), hospitalSetPage.getCurrent(),
                BeanUtils.toBean(hospitalSetPage.getRecords(), HospitalSetVO.class));

        return pageResult;
    }

    @Override
    public Object getInfo(Long id) {

        HospitalSet hospitalSet = getById(id);
        return BeanUtils.toBean(hospitalSet, HospitalSetVO.class);
    }

    @Override
    public boolean add(HospitalSetAddForm form) {

        //设置状态 1 使用 0 不能使用
        form.setStatus(Boolean.TRUE);
        //签名秘钥
        Random random = new Random();
        form.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));

        return save(BeanUtils.toBean(form, HospitalSet.class));
    }

    @Override
    public boolean edit(HospitalSetEditForm form) {

        HospitalSet bean = BeanUtils.toBean(form, HospitalSet.class);
        return updateById(bean);
    }

    @Override
    public void batchRemove(List<Long> idList) {

        baseMapper.deleteByIds(idList);
    }

    @Override
    public void del(Long id) {

        baseMapper.deleteById(id);

    }

}
